package com.wjbgn.bsolver.user.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjbgn.bsolver.user.convert.UserDoConvert;
import com.wjbgn.bsolver.user.dto.UserDTO;
import com.wjbgn.bsolver.user.entity.UserDO;
import com.wjbgn.bsolver.user.service.UserService;
import com.wjbgn.stater.dto.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description:
 * Create Date: 2022-03-11T10:11:42.594
 *
 * @author wjbgn
 * @version 1.0
 */
@Api(tags = "")
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    /**
     * UserService
     */
    private UserService userService;

    /**
     * 分页列表
     *
     * @param userDTO
     * @return
     */
    @ApiOperation(value = "分页列表")
    @GetMapping("/pageList")
    public Result pageList(UserDTO userDTO) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>(UserDoConvert.dtoToDo(userDTO));
        Page<UserDO> page = new Page<>(userDTO.getPage(), userDTO.getLimit());
        Page<UserDO> pageList = userService.page(page, queryWrapper);
        return Result.success(UserDoConvert.pageConvert(pageList));
    }

    /**
     * list列表
     *
     * @param userDTO
     * @return
     */
    @ApiOperation(value = "list列表")
    @GetMapping("/list")
    public Result list(UserDTO userDTO) {
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>(UserDoConvert.dtoToDo(userDTO));
        List<UserDO> userList = userService.list(queryWrapper);
        return Result.success(UserDoConvert.listConvert(userList));
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询")
    @GetMapping("/info/getById")
    public Result info(@RequestParam("id") Long id) {
        UserDO user = userService.getById(id);
        return Result.success(null == user ? null : UserDoConvert.doToDto(user));
    }

    /**
     * 根据用户名查询
     *
     * @param username
     * @return
     */
    @ApiOperation(value = "根据用户名查询")
    @GetMapping("/getUserByUsername")
    public Result getUserByUsername(@RequestParam("username") String username) {
        UserDO user = userService.getOne(new QueryWrapper<>(new UserDO(username)));
        return Result.success(user);
    }
    /**
     * 新增
     *
     * @param userDTO
     * @return
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public Result save(@RequestBody UserDTO userDTO) {
        return userService.saveUser(UserDoConvert.dtoToDo(userDTO));
    }

    /**
     * 更新
     *
     * @param userDTO
     * @return
     */
    @ApiOperation(value = "根据id更新")
    @PostMapping("/updateById")
    public Result update(@RequestBody UserDTO userDTO) {
        boolean flag = userService.updateById(UserDoConvert.dtoToDo(userDTO));
        if (flag) {
            return Result.success();
        }
        return Result.failed();
    }


    /**
     * 更新
     *
     * @param userDTO
     * @return
     */
    @ApiOperation(value = "根据username更新")
    @PostMapping("/updateByUsername")
    public Result updateByUsername(@RequestBody UserDTO userDTO) {
        if (StringUtils.isEmpty(userDTO.getUsername())) {
            return Result.failed("username is null");
        }
        boolean update = userService.update(UserDoConvert.dtoToDo(userDTO), new QueryWrapper<UserDO>(new UserDO(userDTO.getUsername())));
        if (update) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 验证用户账号密码是否正确
     *
     * @param userDTO
     * @return
     */
    @ApiOperation(value = "验证用户账号密码是否正确")
    @PostMapping("/validateUser")
    public boolean validateUser(@RequestBody UserDTO userDTO) {
        List<UserDO> list = userService.list(new QueryWrapper<>(UserDoConvert.dtoToDo(userDTO)));
        if (CollectionUtil.isEmpty(list)) {
            return false;
        }
        return true;
    }
}